package com.iyoudoock.citymine.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iyoudoock.citymine.BaseActivity;
import com.iyoudoock.citymine.R;
import com.iyoudoock.citymine.activity.order.OrderCenterActivity;
import com.iyoudoock.citymine.activity.order.UpdatePriceActivity;
import com.iyoudoock.citymine.activity.user.ChangeIdentityActivity;
import com.iyoudoock.citymine.activity.user.EditRecyclerInfoActivity;
import com.iyoudoock.citymine.constants.HttpUtils;
import com.iyoudoock.citymine.constants.Key;
import com.iyoudoock.citymine.constants.Value;
import com.kvin.toolkit.utils.JumpUtils;
import com.kvin.toolkit.utils.LogUtils;
import com.kvin.toolkit.utils.ToastUtils;
import com.kvin.toolkit.utils.http.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {

    @BindView(R.id.person_info_container)
    LinearLayout personInfoContainer;
    @BindView(R.id.update_price_container)
    LinearLayout updatePriceContainer;
    @BindView(R.id.my_order_container)
    LinearLayout myOrderContainer;
    @BindView(R.id.share_container)
    LinearLayout shareContainer;
    @BindView(R.id.change_identity_container)
    LinearLayout changeIdentityContainer;
    @BindView(R.id.recycle_bin_container)
    LinearLayout recycleBinContainer;
    @BindView(R.id.accept_order_container)
    RelativeLayout acceptOrderContainer;
    @BindView(R.id.logout_container)
    RelativeLayout logoutContainer;
    @BindView(R.id.accept_order_tv)
    TextView acceptOrderTv;

    private boolean isReady = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @Override
    public void initNav() {
        setTitle(getString(R.string.app_name));
        setNavBackVisibility(View.GONE);
    }

    @Override
    public void init() {
        super.init();
        LogUtils.d("----MapAct==null?");
        registerNetReceiver();
        mJsonUtils = JsonUtils.getInstance();
        this.personInfoContainer.setOnClickListener(this);
        this.updatePriceContainer.setOnClickListener(this);
        this.myOrderContainer.setOnClickListener(this);
        this.shareContainer.setOnClickListener(this);
        this.changeIdentityContainer.setOnClickListener(this);
        this.recycleBinContainer.setOnClickListener(this);
        this.acceptOrderContainer.setOnClickListener(this);
        this.logoutContainer.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.person_info_container:
                JumpUtils.simpleJump(getActivity(), EditRecyclerInfoActivity.class);
                break;
            case R.id.update_price_container:
                JumpUtils.simpleJump(getActivity(), UpdatePriceActivity.class);
                break;
            case R.id.my_order_container:
                mIntent = new Intent(getActivity(), OrderCenterActivity.class);
                mIntent.putExtra(Key.Tag.FROM, Value.Act.MAIN);
                JumpUtils.simpleJump(getActivity(), mIntent);
                break;
            case R.id.share_container:
                JumpUtils.simpleJump(getActivity(), ShareActivity.class);
                break;
            case R.id.change_identity_container:
                JumpUtils.simpleJump(getActivity(), ChangeIdentityActivity.class);
                break;
            case R.id.recycle_bin_container:
                JumpUtils.simpleJump(getActivity(), RecycleBinActivity.class);
                break;
            case R.id.accept_order_container:
                resetOrderView();
                break;
            case R.id.logout_container:
                logout();//jump to map page when successfully
                break;
        }
    }


    @Override
    public void onSuccess(JSONObject response, String flag) {
        super.onSuccess(response, flag);
        if (response == null) return;
        try {
            int state = response.getInt(Key.Tag.STATE);
            if (state == Value.Response.SUCCESS && flag.equals(Value.Flag.START_ACCEPT_ORDER)) {
                ToastUtils.simpleToast(getActivity(), "开始接单");
            } else if (state == Value.Response.SUCCESS && flag.equals(Value.Flag.STOP_ACCEPT_ORDER)) {
                ToastUtils.simpleToast(getActivity(), "暂停接单");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(IOException e, String flag) {
        super.onFailure(e, flag);
    }

    /**
     * reset order view
     */
    private void resetOrderView() {
        if (isReady) {
            isReady = false;
            params = HttpUtils.launchAcceptOrder(mUid, Value.Flag.START_ACCEPT);
            mJsonUtils.post(HttpUtils.LAUNCH_ACCEPT_URL, params, this, Value.Flag.START_ACCEPT_ORDER);
            this.acceptOrderContainer.setBackgroundResource(R.drawable.main_item_green);
            this.acceptOrderTv.setText("准备接单中");
            this.acceptOrderTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.stop_receive_order, 0, 0, 0);
        } else {
            isReady = true;
            params = HttpUtils.launchAcceptOrder(mUid, Value.Flag.STOP_ACCEPT);
            mJsonUtils.post(HttpUtils.LAUNCH_ACCEPT_URL, params, this, Value.Flag.STOP_ACCEPT_ORDER);
            this.acceptOrderContainer.setBackgroundResource(R.drawable.main_item_orange);
            this.acceptOrderTv.setText("暂停接单中");
            this.acceptOrderTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.start_receive_order, 0, 0, 0);
        }
    }


    @Override
    public void onBackPressed() {
        pressAgainExit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterNetReceiver();
        //logout();//logout
    }

    @Override
    public void onHandleMessage(Message msg) {
        super.onHandleMessage(msg);
        switch (msg.what) {
            case Value.Message.LOGOUT_SUCCESS:
                JumpUtils.simpleJump(getActivity(), MapActivity.class, true);
                break;
        }
    }
}
